//var tagsDocument="";
function caiji(){
	//alert(imageStr);
	 //var maktemp='<div id="markdiag" style="background:#666;width:300; height:200px; position:absolute; top:0px; left:0px; z-index:80;"></div>';
	
	var head = document.getElementsByTagName("head")[0];       
	 var js = document.createElement("script");       
	 js.src = "http://www.manlost.com:9090/manlost/collection.htm";       
	 js.onload = js.onreadystatechange = function()       
	 {
	     //alert(1);
		 if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")       
	     {   
	        // alert(2); 
			 head.removeChild(js); 
			 if(checkImage()){
				openNewDiv("div");
			 }else{
				 alert("没有足够大的图片。");
			 }
			 
				//alert(tagsDocument);
	     }
	 }       
	 head.appendChild(js);
	
	 //alert(1);
	 
	 /*var doc;

	 if (document.all){//IE
		 doc = document.frames["caijiFrame"].document;
	 }else{//Firefox
		 doc = document.getElementById("caijiFrame").contentDocument;
	 } */
	 //alert(document.getElementById("caijiFrame").document);
	 //showImage();
	 //alert(2);
	//add();
	 
	
}
var min_width=80;
var min_height=80;
var tip_height=40;
function checkImage(){
	var div= document.getElementById("div");
	var images = document.getElementsByTagName("img");
	for(var i=0;i<images.length;i++){
		if(images[i].width>min_width&&images[i].height>min_height){
			return true;
		}
	}
	return false;
}

function showImage(){
	var div= document.getElementById("div");
	var images = document.getElementsByTagName("img");
	var imageStr = '<div style="margin-top:'+tip_height+'px;">';
	for(var i=0;i<images.length;i++){
		/*var strs='<div class="';
		strs+="-moz-border-bottom-colors: none;-moz-border-image: none;-moz-border-left-colors: none;-moz-border-right-colors: none;-moz-border-top-colors: none;";
		strs+="background-color: #FFFFFF;border-color: #E7E7E7;border-style: solid; border-width: 0 1px 1px 0;float: left;height: 200px;";
		strs+="margin: 0;opacity: 1;overflow: hidden;padding: 0;position: relative;text-align: center;width: 200px;z-index: 10002;";
		strs+='">';
		strs+='<span class="background: none repeat scroll 0 0 white;border-radius: 4px 4px 4px 4px;color: #000000;display: inline-block;font-size: 10px;margin-top: 180px;padding: 0 2px;position: relative;text-align: center;z-index: 10003;">192 x 144</span>';
	    strs+='<div class="huabanImg">'
	    strs+='<a href="javascript:;">';
	    strs+='<img class="ImageToPin" style="margin-top: 28px;" src="'+images[i].src+'" alt="Pin This">';
	    strs+='<img class="huaban_PinIt" src="http://huaban.com/img/PinThis.png" alt="Pin This" style="display: none;">';
	    strs+='</a>';
	    strs+='</div>';
	    strs+='</div>';
	    imageStr+=strs;*/
		if(images[i].width>min_width&&images[i].height>min_height){
		var strs='<div style="float:left;width:200px;height:200px;background:#fff;text-align:center;border-width:1px; border-color:#999; border-style:solid;"><img style="cursor:pointer;height: auto !important;max-height: 200px;max-width: 200px;width: auto !important;" onclick="hh(this)" src="'+images[i].src+'" /><br/>'+images[i].width+'X'+images[i].height+'<input type="checkbox" name="images_checkbox" value="'+images[i].src+'"></input></div>';
	    imageStr+=strs;
		}
	   //imageStr+="<img src='"+images[i].src+"'/><img class='huaban_PinIt' src='http://huaban.com/img/PinThis.png' alt='Pin This' style='display: block;'>";
	}
	imageStr+="</div>";
	return imageStr;
}

function getManlostTags(){
	//alert(tagsDocument);
	var tagsDocument="";
	var tags=getTags();
	//alert(tags);
	var groups=eval(tags);
	for(var i=0;i<groups.length;i++){
		group=groups[i];
		tagsDocument+="<div><span>"+group.name+"</span>";
		//alert(group.id+"------"+group.name);
		for(var j=0;j<group.tags.length;j++){
			tag=group.tags[j];
			tagsDocument+="<span>"+tag.name+"</span>";
			//alert(tag.id+"----"+tag.name);
		}
		tagsDocument+="</div>";
	} 
	tagsDocument+="<div><form name='caijiForm' id='caijiForm' method='post' action='http://www.manlost.com:9090/manlost/addCollection.htm'><textarea id='images_urls' name='imageUrls'></textarea><textarea id='ok_tags' name='okTags'></textarea><input type='button' onclick='confirmCaiji();' value='采集'></input></form></div>";
	//alert(tagsDocument);
	return tagsDocument;
}

function confirmCaiji(){
	var images_checkbox=document.getElementsByName("images_checkbox");
	for(var i=0;i<images_checkbox.length;i++){
		if(images_checkbox[i].checked){
			document.getElementById("images_urls").value+=images_checkbox[i].value+";";
		}
	}
	document.getElementById("ok_tags").value="2;3";
	document.caijiForm.submit();
}

function getIt(){
	alert(window.parent);
}

function hh(img){
	var image=encodeURIComponent(img.src);
	window.open ('http://www.manlost.com:9090/manlost/collection.htm?img='+image+'', 'newwindow', 'height=500, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no') ;
}
var docEle = function() {
	return document.getElementById(arguments[0]) || false;
	}
	function openNewDiv(_id) {
	var m = "mask";
	if (docEle(_id)) document.removeChild(docEle(_id));
	if (docEle(m)) document.removeChild(docEle(m));
	// 新激活图层
	var newDiv = document.createElement("div");
	newDiv.id = _id;
	newDiv.style.position = "absolute";
	newDiv.style.float="left";
	newDiv.style.zIndex = "9999";
	newDiv.style.width = document.body.scrollWidth + "px";
	newDiv.style.height = document.body.scrollHeight + "px";
	newDiv.style.top = "0px";
	newDiv.style.left = "0px";//(parseInt(document.body.scrollWidth) - 300) / 2 + "px"; // 屏幕居中
	newDiv.style.background = "#EFEFEF";
	newDiv.style.border = "1px solid #860001";
	
	var leftDiv=document.createElement("div");
	if(document.all){
		leftDiv.style.styleFloat = 'left';
	}else{
		leftDiv.style.cssFloat = 'left';
	}
	leftDiv.style.width="70%";
	leftDiv.style.top=tip_height+"px";
	//leftDiv.style.width="500px";
	leftDiv.innerHTML=showImage();
	newDiv.appendChild(leftDiv);
	var rightDiv=document.createElement("div");
	if(document.all){
		rightDiv.style.styleFloat = 'left';
	}else{
		rightDiv.style.cssFloat = 'left';
	}
	rightDiv.style.padding=tip_height+"px";
	rightDiv.innerHTML=getManlostTags();
	rightDiv.style.top=tip_height+"px";
	rightDiv.style.width="200px";
	rightDiv.style.margin = "5px";
	newDiv.appendChild(rightDiv);
	/*var left = document.createElement("div");
	left.innerHTML=showImage();
	var right=document.createElement("div");
	right.style.width="200px";
	right.innerHTML("tags");
	newDiv.appendChild(left);
	newDiv.appendChild(right);*/
	/*newDiv.innerHTML =showImage();
	var rightDiv=document.createElement("div");
	newDiv.style.position = "absolute";
	rightDiv.style.zIndex = "9999";
	rightDiv.style.float="right";
	rightDiv.style.width="200px";
	rightDiv.innerHTML="tags";
	rightDiv.style.background = "#EFEFEF";
	rightDiv.style.border = "1px solid #860001";
	rightDiv.style.padding = "5px";

	*/
	//alert(1);
	document.body.appendChild(newDiv);
	
	/*var ifr = document.createElement("IFRAME"); 
	ifr.id="caijiFrame";
	 ifr.frameBorder = 0; 
	 ifr.scrolling = "no";  
	 ifr.src = "http://www.manlost.com:9090/manlost/collection.htm";  
	 newDiv.appendChild(ifr);  
	 ifr.width = 400; 
	 ifr.height = 400;
	//alert(2);
*/	// mask图层
	var newMask = document.createElement("div");
	newMask.id = m;
	newMask.style.position = "absolute";
	newMask.style.zIndex = "1";
	newMask.style.width = document.body.scrollWidth + "px";
	newMask.style.height = document.body.scrollHeight + "px";
	newMask.style.top = "0px";
	newMask.style.left = "0px";
	newMask.style.background = "#000";
	newMask.style.filter = "alpha(opacity=40)";
	newMask.style.opacity = "0.40";
	document.body.appendChild(newMask);
	// 关闭mask和新图层
	var newA = document.createElement("div");
	newA.innerHTML = "<a href='#'>取消</a>";
	newA.style.height=""+tip_height+"px";
	newA.style.width="100%";
	newA.style.cursor="pointer";
	newA.style.text="center";
	newA.style.position = "absolute";
	newA.style.background= "#F2F2F2";
	newA.style.top = "5px";
	newA.style.left = "33px";
	if(document.all){
		newA.style.styleFloat = 'clear:both';
	}else{
		newA.style.cssFloat = 'clear:both';
	}
	newA.onclick = function() {
	document.body.removeChild(docEle(_id));
	document.body.removeChild(docEle(m));
	return false;
	}
	newDiv.appendChild(newA);
	}